package cn.com.dcsgo.springcloudstudy.payment;

import cn.com.dcsgo.springcloudstudy.commons.utils.PortUtil;
import com.netflix.hystrix.contrib.metrics.eventstream.HystrixMetricsStreamServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;

/**
 * 启动类
 *
 * @author Dcsgo
 * @since 2024-08-06
 */
@EnableEurekaClient
@SpringBootApplication
@EnableCircuitBreaker//激活降级容错
public class PaymentHystrixMain8001 {
    public static void main(String[] args) {
        int port = PortUtil.findFreePort();
        System.out.println("Starting service on port: " + port);
        SpringApplication.run(PaymentHystrixMain8001.class, "--server.port=" + port);
    }

    /**
     * 服务监控配置
     *
     * @return ServletRegistrationBean<HystrixMetricsStreamServlet>
     */
    @Bean
    public ServletRegistrationBean<HystrixMetricsStreamServlet> getServlet(){
        HystrixMetricsStreamServlet streamServlet = new HystrixMetricsStreamServlet();
        ServletRegistrationBean<HystrixMetricsStreamServlet> registrationBean = new ServletRegistrationBean<>(streamServlet);
        registrationBean.setLoadOnStartup(1);
        registrationBean.addUrlMappings("/hystrix.stream");
        registrationBean.setName("HystrixMetricsStreamServlet");
        return registrationBean;
    }
}
